//
//  BundleResource.swift
//  TTCOM-macOS
//
//  Created by Toj on 3/29/21.
//

import Foundation

open class BundleResource: NSObject {
    
    public static let shared = BundleResource()
    private var bundles: [String: Bundle] = [:]
    
    public func url(source name: String, ext: String?) -> Bundle? {
        
        var bundleName = name
        let type = ext ?? ""
        if !bundleName.contains(type) {
            bundleName = bundleName + type
        }
        
        var bundle = bundles[bundleName]
        guard bundle == nil else { return bundle }
   
        let myBundle = Bundle(for: BundleResource.self)
        guard let url = myBundle.url(forResource: name, withExtension: ext) else { return nil }
        
        bundle = Bundle(url: url)
        bundles[bundleName] = bundle
        return bundle
    }
}
